package com.zyy.smartcity1129.citysubway;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.MetroInfo;
import com.zyy.smartcity1129.data.SubwayMainInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.List;

/**
 * @ClassName: Subway1Fragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 3/12/2021$
 * @Version: 1.0
 */
public class Subway1Fragment extends BaseFragment {
    private SubwayMainInfo.DataBean subway = CitySubwayFragment.subway;
    private TextView tvSubway1SE;
    private TextView tvSubway1Time;
    private TextView tvSubway1Station;
    private TextView tvSubway1Km;
    private RecyclerView rvSubway1;
    private int lineId = subway.getLineId();

    @Override
    protected int layId() {
        return R.layout.f_subway1;
    }

    @Override
    protected String title() {
        imgRight.setVisibility(View.VISIBLE);
        imgRight.setImageResource(R.drawable.linemap);
        imgRight.setColorFilter(Color.BLACK);
        return subway.getLineName();
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        rvSubway1.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
        th(() -> {
            MetroInfo.DataBean metro = Http.getInstance().get("/prod-api/api/metro/line/" + lineId, null, MetroInfo.class, 1).getData();
            List<MetroInfo.DataBean.MetroStepListBean> stepList = metro.getMetroStepList();
            RvAd<MetroInfo.DataBean.MetroStepListBean> rvAd = new RvAd<MetroInfo.DataBean.MetroStepListBean>(stepList, R.layout.rv_metro) {
                private void initView(View vi) {
                    tvRvMetroName = (TextView) vi.findViewById(R.id.tvRvMetroName);
                    imgRvMetro = (ImageView) vi.findViewById(R.id.imgRvMetro);
                }

                private ImageView imgRvMetro;
                private TextView tvRvMetroName;

                @Override
                protected void initR(View vi, MetroInfo.DataBean.MetroStepListBean metroStepListBean, int p) {
                    initView(vi);
                    tvRvMetroName.setText(metroStepListBean.getName());
                    if (metroStepListBean.getName().equals("建国门")) {
                        tvRvMetroName.setTextColor(ContextCompat.getColor(getContext(), R.color.sell));
                    }
                    if (metroStepListBean.getName().equals(metro.getRunStationsName())) {
                        imgRvMetro.setBackgroundResource(R.drawable.metro);
                    }
                }
            };
            ui(() -> {
                rvSubway1.setAdapter(rvAd);
                tvSubway1SE.setText(metro.getFirst() + "—" + metro.getEnd());
                tvSubway1Time.setText("剩余时间：" + metro.getRemainingTime() + "min");
                tvSubway1Station.setText("间隔站数：" + metro.getStationsNumber() + "站");
                tvSubway1Km.setText("剩余距离：" + metro.getKm() + "km");
            });
        });
        imgRight.setOnClickListener(view -> {
            add(new LineMapFragment());
        });
    }

    private void initView(View iv) {
        tvSubway1SE = (TextView) iv.findViewById(R.id.tvSubway1SE);
        tvSubway1Time = (TextView) iv.findViewById(R.id.tvSubway1Time);
        tvSubway1Station = (TextView) iv.findViewById(R.id.tvSubway1Station);
        tvSubway1Km = (TextView) iv.findViewById(R.id.tvSubway1Km);
        rvSubway1 = (RecyclerView) iv.findViewById(R.id.rvSubway1);
    }
}
